Assignment for RMIT Mixed Reality in 2020
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

76 lines
2.3 KiB

  1. namespace VRTK.Examples
  2. {
  3. using UnityEngine;
  4. using UnityEngine.UI;
  5. using VRTK.Controllables;
  6. public class TogglePointerInteraction : MonoBehaviour
  7. {
  8. public enum OptionType
  9. {
  10. InteractWithObjects,
  11. GrabToPointerTip
  12. }
  13. public OptionType optionType = OptionType.InteractWithObjects;
  14. public VRTK_Pointer[] pointers = new VRTK_Pointer[0];
  15. public VRTK_BaseControllable controllable;
  16. public Text displayText;
  17. public string maxText;
  18. public string minText;
  19. protected virtual void OnEnable()
  20. {
  21. controllable = (controllable == null ? GetComponent<VRTK_BaseControllable>() : controllable);
  22. if (controllable != null)
  23. {
  24. controllable.MaxLimitReached += MaxLimitReached;
  25. controllable.MinLimitReached += MinLimitReached;
  26. }
  27. }
  28. protected virtual void OnDisable()
  29. {
  30. if (controllable != null)
  31. {
  32. controllable.MaxLimitReached -= MaxLimitReached;
  33. controllable.MinLimitReached -= MinLimitReached;
  34. }
  35. }
  36. protected virtual void MaxLimitReached(object sender, ControllableEventArgs e)
  37. {
  38. SetOption(true, maxText);
  39. }
  40. protected virtual void MinLimitReached(object sender, ControllableEventArgs e)
  41. {
  42. SetOption(false, minText);
  43. }
  44. protected virtual void SetOption(bool value, string text)
  45. {
  46. if (displayText != null)
  47. {
  48. displayText.text = text;
  49. }
  50. foreach (VRTK_Pointer pointer in pointers)
  51. {
  52. pointer.enabled = false;
  53. pointer.pointerRenderer.enabled = false;
  54. switch (optionType)
  55. {
  56. case OptionType.InteractWithObjects:
  57. pointer.interactWithObjects = value;
  58. break;
  59. case OptionType.GrabToPointerTip:
  60. pointer.grabToPointerTip = value;
  61. break;
  62. }
  63. pointer.pointerRenderer.enabled = true;
  64. pointer.enabled = true;
  65. }
  66. }
  67. }
  68. }